package cn.sdc.bugKiller.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import cn.sdc.bugKiller.util.SecurityUtil;

public class SecurityFilter implements Filter {

	
	private static final String LOGON_URI="login.html";
    private static final String HOME_URI="home_uri";
    
    private String logon_page;    
    private String home_page;
    
//	private static CodeDealService codeService = new CodeDealService();    
//	private static LoginService loginService = new LoginService(); 
//	
	@Override
	public void init(FilterConfig fc) throws ServletException {
//        logon_page=fc.getInitParameter(LOGON_URI);
//        home_page=fc.getInitParameter(HOME_URI);
//        
//        if(null==logon_page || null==home_page)
//            throw new ServletException("There is no Login uri or home uri！");
    }
	@Override
	public void destroy() {

	}

	@Override
	public void doFilter(ServletRequest req, ServletResponse resp,
			FilterChain fc) throws IOException, ServletException {
		
        //将请求对象和响应对象的类型转换为
        //HttpServletRequest和HttpServletResponse。
        HttpServletRequest httpReq=(HttpServletRequest)req;
        HttpServletResponse httpResp=(HttpServletResponse)resp;
        HttpSession session=httpReq.getSession();
        
        //得到用户的请求URI。
        String request_uri = httpReq.getRequestURI();
        //得到Web应用程序的上下文路径。
        String ctxPath=httpReq.getContextPath();
        //去除上下文路径，得到剩余部分的路径。
        String uri=request_uri.substring(ctxPath.length());

		if(uri.indexOf("log") == -1 && !SecurityUtil.isLogon()){
			req.getRequestDispatcher("/login.jsp").forward(req, resp);
			return;
		}
		fc.doFilter(req, resp);
	}


}
